﻿package com.firewolf.iteratorAndComponent.combine;

import java.util.Iterator;
import java.util.Stack;

@SuppressWarnings("rawtypes")
public class CompositeIterator implements Iterator {
	Stack stack = new Stack();
	@SuppressWarnings("unchecked")
	public CompositeIterator(Iterator<MenuComponent> iterator) {
		stack.push(iterator);
	}

	@Override
	public boolean hasNext() {
		if(stack.empty()){
			return false ;
		}else{
			Iterator iterator = (Iterator) stack.peek();
			if(!iterator.hasNext()){
				stack.pop();
				return hasNext();
			}else{
				return true;
			}
		}
	}

	@SuppressWarnings("unchecked")
	@Override
	public Object next() {
		if(hasNext()){
			Iterator iterator = (Iterator) stack.peek();
			MenuComponent component = (MenuComponent) iterator.next();
			if(component instanceof MyMenu){
				stack.push(component.getIterator());
			}
			return component ;
		}else {
			return null ;
		}
	}
	@Override
	public void remove() {
		throw new UnsupportedOperationException();
	}
}
